#include "StdAfx.h"
#include "ChmDisplay.h"

using namespace HapticPuppy;
using namespace System::Windows::Forms;

ChmDisplay::ChmDisplay(System::Windows::Forms::Panel ^ parentPanel ) {
	CreateParams^ cp = gcnew CreateParams;

	// Set the position on the form
	cp->X = 100;
	cp->Y = 100;
	cp->Height = parentPanel->Width;
	cp->Width = parentPanel->Height;

	// Specify the panel as the parent.
	cp->Parent = parentPanel->Handle;

	// Create as a child of the specified parent and make OpenGL compliant (no clipping)
	cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

	// Create the actual window
	this->CreateHandle(cp);

	hdc = GetDC((HWND)this->Handle.ToPointer());

	if(hdc)
		MySetPixelFormat(hdc);
}

ChmDisplay::~ChmDisplay(System::Void) {
	this->DestroyHandle();
}

System::Void ChmDisplay::Render(System::Void) {
	// Clear the color and depth buffers.
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

System::Void ChmDisplay::ReSizeGLScene(GLsizei width, GLsizei height)	// Resize and initialise the gl window
{
	if (height==0)										// Prevent A Divide By Zero By
	{
		height=1;										// Making Height Equal One
	}

	// Resize the native window containing the glViewport
	SetWindowPos((HWND)this->Handle.ToPointer(), nullptr, 0, 0, width, height, 0);

	glViewport(0,0,width,height);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}

System::Void ChmDisplay::SwapOpenGLBuffers(System::Void) {
	SwapBuffers(hdc);
}

GLint ChmDisplay::MySetPixelFormat(HDC hhdc) {
	PIXELFORMATDESCRIPTOR pfd = { 
		sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd 
		1,                                // version number 
		PFD_DRAW_TO_WINDOW |              // support window 
		PFD_SUPPORT_OPENGL |              // support OpenGL 
		PFD_DOUBLEBUFFER,                 // double buffered 
		PFD_TYPE_RGBA,                    // RGBA type 
		24,                               // 24-bit color depth 
		0, 0, 0, 0, 0, 0,                 // color bits ignored 
		0,                                // no alpha buffer 
		0,                                // shift bit ignored 
		0,                                // no accumulation buffer 
		0, 0, 0, 0,                       // accum bits ignored 
		32,                               // 32-bit z-buffer     
		0,                                // no stencil buffer 
		0,                                // no auxiliary buffer 
		PFD_MAIN_PLANE,                   // main layer 
		0,                                // reserved 
		0, 0, 0                           // layer masks ignored 
	}; 
			
	GLint  iPixelFormat; 
		 
	// get the device context's best, available pixel format match 
	if((iPixelFormat = ChoosePixelFormat(hhdc, &pfd)) == 0)
	{
		MessageBox::Show("ChoosePixelFormat Failed");
		return 0;
	}
			 
	// make that match the device context's current pixel format 
	if(SetPixelFormat(hhdc, iPixelFormat, &pfd) == FALSE)
	{
		MessageBox::Show("SetPixelFormat Failed");
		return 0;
	}

	if((hglrc = wglCreateContext(hdc)) == NULL)
	{
		MessageBox::Show("wglCreateContext Failed");
		return 0;
	}
			
	if((wglMakeCurrent(hdc, hglrc)) == NULL)
	{
		MessageBox::Show("wglMakeCurrent Failed");
		return 0;
	}


	return 1;
}

